<?php 
	namespace Admin\Model;
	use Think\Model;
	use \Think\Upload;
	class CarModel extends Model{
		protected $lassError='';
		public function getLastError(){
			return $this->lassError;
		}
		
		protected function upload($file){
		  $upload = new \Think\Upload();// 实例化上传类
		  $upload->maxSize=  3145728 ;// 设置附件上传大小
		  $upload->exts=  array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		  $upload->rootPath=  C('UPLOAD_ROOT_PATH'); // 设置附件上传根目录
		// 上传单个文件 
		  $info   =   $upload->uploadOne($file);
		 if(!$info) {// 上传错误提示错误信息
			 $this->lassError=$upload->getError();
			 return false;
		  }else{// 上传成功 获取上传文件信息
		 	 $path=$info['savepath'].$info['savename'];
			 return $path;
			}
		}
	
	
		public function addCar($name,$price,$number_of_people,$number_of_baggage,$car_operation,$describe,$file){

			if($file['error']==!0){
				$this->lassError='上传文件错误，错误代码为'.$file['error'];
				return false;
			}
			$pic=$this->upload($file);
			$data=$this->checkData($name,$price,$number_of_people,$number_of_baggage,$car_operation,$describe);
			$data['pic']=$pic;
			$data['add_time']=time();
			
			$this->create($data);
			$carId=$this->add();
			if($carId==false){
				$this->lassError='添加记录失败';
				return false;
			}
			return $carId;
			
			
		}
		
		public function getCarList($page='1',$limit='10'){
			$carList=$this->page($page,$limit)->select();
			return $carList;
		}
		
		public function getCarCount(){
			$count=$this->count();
			return $count;
		}
		
		public function deleteCar($carId){
			$affectRow=$this->where('car_id=%d',$carId)->delete();
			if($affectRow==false){
				$this->lassError='删除失败';
			}
			return $affectRow;
		}
		
		public function getCarById($carId){
			$carInfo=$this->where('car_id=%d',$carId)->find();
			if($carInfo=== null){
				$this->lassError='没找到内容';
				
			}
			if($carInfo=== false){
				$this->lassError='mysql 错误';
			}
			return $carInfo;
		}
		
		public function updateCar($carId,$name,$price,$number_of_people,$number_of_baggage,$car_operation,$describe,$file=null){
			$data=$this->checkData($name,$price,$number_of_people,$number_of_baggage,$car_operation,$describe);
			if($file['error']===0){
				$pic=$this->upload($file);
				$data['pic']=$pic;
			}
			$this->create($data);
			$affectRow=$this->where('car_id=%d',$carId)->save();
			if($affectRow===0){
				$this->lassError='数据未发生变化';
			}
			if($affectRow===false){
				$this->lassError='数据库错误';
			}
			return $affectRow;
			
		}
		
		protected function checkData($name,$price,$number_of_people,$number_of_baggage,$car_operation,$describe=''){
			if(empty($name)){
				$this->lassError='汽车名字不能为空';
				return false;
			}
			if(empty($price) && !is_numeric($price)){
				$this->lassError='汽车价格不能为空';
				return false;
			}
			if(empty($number_of_people) && !is_int($number_of_people)){
				$this->lassError='汽车座位数不能为空';
				return false;
			}
			if(!isset($number_of_baggage) && !is_int($number_of_baggage)){
				$this->lassError='汽车可携带行李数';
				return false;
			}
			if(!isset($car_operation)){
				$this->lassError='汽车档位未选择';
				return false;
			}
/* 			if(empty($describe)){
				$this->lassError='汽车描述不能为空';
				return false;
			}
			 */
			$data=array();
			$data['name']=$name;
			$data['price']=$price;
			$data['describe']=$describe;
			$data['number_of_people']=$number_of_people;
			$data['number_of_baggage']=$number_of_baggage;
			$data['car_operation']=$car_operation;
			return $data;
		}
		
		public function getFreeCarList($busyCarList,$page=1,$limit=10){
			if(empty($busyCarList)){
			  $freeCarList=$this->page($page,$limit)->select();
			  return $freeCarList;
			}
			 $busyCarList=implode(',',$busyCarList);
			 $freeCarList=$this->where('car_id not in (%s)',$busyCarList)->page($page,$limit)->select();
			return $freeCarList;
		}
		
	}